package com.demo.common.excel;

import com.demo.common.vo.Result;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;

/**
 * excel公共controller
 */
@Component
@RestController
@RequestMapping("/excel")
@Slf4j
public class ExcelController{

    @Resource
    private IExcelOperateService excelOperateService;

    /**
     * 获取导入模板
     * @param scene
     * @param response
     */
    @GetMapping("/template/v1")
    @SneakyThrows
    public void template(@RequestParam("scene")String scene, HttpServletResponse response){
        excelOperateService.createTemplate(scene,response);
    }

    /**
     * 导入数据
     * @param scene
     * @param file
     * @return
     */
    @PostMapping("/import/v1")
    @SneakyThrows
    public Result importData(@RequestParam("scene")String scene, @RequestParam("file")MultipartFile file){
        excelOperateService.importData(scene,file.getInputStream());
        return Result.success("success");
    }

    /**
     * 导出数据
     * @param scene
     * @param params
     * @param response
     */
    @PostMapping("/export/v1")
    @SneakyThrows
    public void exportData(@RequestParam("scene")String scene, @RequestBody Map<String,Object> params,HttpServletResponse response){
        excelOperateService.exportData(scene,params,response);
    }

}
